package cn.com.ege.mvc.action.business;

import java.io.Serializable;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.ege.mvc.controller.BaseAction;
import cn.com.ege.mvc.controller.IAction;
import cn.com.ege.mvc.exception.BusinessException;
import cn.com.ege.mvc.model.business.Material;
import cn.com.ege.mvc.model.business.Purchase;
import cn.com.ege.mvc.model.business.PurchaseDetail;
import cn.com.ege.mvc.model.business.Supplier;
import cn.com.ege.mvc.model.view.PurchaseDetailView;
import cn.com.ege.mvc.service.impl.business.MaterialService;
import cn.com.ege.mvc.service.impl.business.PurchaseDetailService;
import cn.com.ege.mvc.service.impl.business.PurchaseService;
import cn.com.ege.mvc.service.impl.business.SupplierService;
import cn.com.ege.mvc.service.impl.view.PurchaseDetailViewService;
import cn.com.ege.mvc.util.PageController;

public class PurchaseDetailAction extends BaseAction implements IAction,
		Serializable {
	private static final long serialVersionUID = 8299975587235537983L;
	private final static Log logger = LogFactory.getLog(ModelRuleAction.class);
	private PageController pager = null;
	private PurchaseDetail purchaseDetail = null;
	private PurchaseDetailService purchaseDetailService = null;
	private PurchaseDetailView purchaseDetailView = null;
	private PurchaseDetailViewService purchaseDetailViewService = null;
	private List<Material> materialList = null;
	private MaterialService materialService = null;
	private List<Supplier> supplierList = null;
	private SupplierService supplierService = null;

	public String execute(HttpServletRequest request,
			HttpServletResponse response, ServletConfig servletConfig) {
		String act = request.getParameter("act");
		Long purchase_id = Long.parseLong(request.getParameter("purchase_id"));
		request.setAttribute("purchase_id", purchase_id);
		if (null != act) {
			// 取列表
			if ("list".equals(act)) {
				purchaseDetailView.setPurchase_id(purchase_id);
				int totalRows = purchaseDetailViewService
						.getRecordCount(purchaseDetailView);
				String URL = request.getRequestURI();
				this.pager.setURL(URL);
				this.pager.setTotalRowsAmount(totalRows);
				List<PurchaseDetailView> resourceList = purchaseDetailViewService
						.list(purchaseDetailView, this.pager.getPageStartRow(),
								pager.getPageSize(), "order by id desc", true);
				pager.setData(resourceList);
				Purchase purchase = new Purchase();
				purchase.setId(purchase_id);
				purchase = new PurchaseService().get(purchase);
				request.setAttribute("purchase", purchase);
				return getReturnURL(LIST);
			}
			// 点了确认采购
			else if ("comfirm".equals(act)) {
				purchaseDetail = purchaseDetailService.get(purchaseDetail);
				Material material = new Material();
				material.setId(purchaseDetail.getMaterial_id());
				MaterialService materialService = new MaterialService();
				material = materialService.get(material);
				material.setRepertory(material.getRepertory()
						+ purchaseDetail.getAmount());// 材料入库（修改库存）
				purchaseDetail.setStatus(1L);// 改变采购状态
				materialService.update(material);// 完成入库
				purchaseDetailService.update(purchaseDetail);
				return getReturnURL("getList");
			}
			// 点了添加或者点了修改
			else if ("get".equals(act)) {
				purchaseDetailView = purchaseDetailViewService
						.get(purchaseDetailView);
				MaterialService materialService = new MaterialService();
				SupplierService supplierService = new SupplierService();
				materialList = materialService.listAll(new Material());
				supplierList = supplierService.listAll(new Supplier());
				request.setAttribute("materialList", materialList);
				request.setAttribute("supplierList", supplierList);
				return getReturnURL(EDIT);
			}
			// 保存表单
			else if ("save".equals(act)) {
				try {
					String materialDatas = request
							.getParameter("MaterialDatas");
					String[] materialData = materialDatas.split(",");
					purchaseDetail.setMaterial_id(Long
							.parseLong(materialData[0]));
					purchaseDetail.setPurchase_id(purchase_id);
					purchaseDetailService.save(purchaseDetail);
					return getReturnURL("getList");
				} catch (BusinessException e) {
					// 保存原来表单已输入的内容
					request.setAttribute("purchaseDetailView",
							purchaseDetailView);
					addMessage(e.getMessage());
					return getReturnURL(EDIT);
				}
			}
			// 删除
			else if ("del".equals(act)) {
				String[] ids = request.getParameterValues("id");
				for (String Id : ids) {
					PurchaseDetail purchaseDetail = new PurchaseDetail();
					if (null != Id && !("".equals(Id))) {
						purchaseDetail.setId(Long.parseLong(Id));
						purchaseDetailService.delete(purchaseDetail);
					}
				}
				return getReturnURL("getList");
			}
			
			//打印
			else if ("print".equals(act)) {
				purchaseDetailView.setPurchase_id(purchase_id);
				int totalRows = purchaseDetailViewService
						.getRecordCount(purchaseDetailView);
				String URL = request.getRequestURI();
				this.pager.setURL(URL);
				this.pager.setTotalRowsAmount(totalRows);
				List<PurchaseDetailView> resourceList = purchaseDetailViewService
						.list(purchaseDetailView, this.pager.getPageStartRow(),
								pager.getPageSize(), "order by id desc", true);
				pager.setData(resourceList);
				Purchase purchase = new Purchase();
				purchase.setId(purchase_id);
				purchase = new PurchaseService().get(purchase);
				request.setAttribute("purchase", purchase);
				return getReturnURL("print");
			}
		}
		addMessage("参数不正确!! act=" + act);
		return getReturnURL(ERROR);
	}

	public PageController getPager() {
		return pager;
	}

	public void setPager(PageController pager) {
		this.pager = pager;
	}

	public PurchaseDetail getPurchaseDetail() {
		return purchaseDetail;
	}

	public void setPurchaseDetail(PurchaseDetail purchaseDetail) {
		this.purchaseDetail = purchaseDetail;
	}

	public PurchaseDetailService getPurchaseDetailService() {
		return purchaseDetailService;
	}

	public void setPurchaseDetailService(
			PurchaseDetailService purchaseDetailService) {
		this.purchaseDetailService = purchaseDetailService;
	}

	public PurchaseDetailView getPurchaseDetailView() {
		return purchaseDetailView;
	}

	public void setPurchaseDetailView(PurchaseDetailView purchaseDetailView) {
		this.purchaseDetailView = purchaseDetailView;
	}

	public PurchaseDetailViewService getPurchaseDetailViewService() {
		return purchaseDetailViewService;
	}

	public void setPurchaseDetailViewService(
			PurchaseDetailViewService purchaseDetailViewService) {
		this.purchaseDetailViewService = purchaseDetailViewService;
	}

	public List<Material> getMaterialList() {
		return materialList;
	}

	public void setMaterialList(List<Material> materialList) {
		this.materialList = materialList;
	}

	public MaterialService getMaterialService() {
		return materialService;
	}

	public void setMaterialService(MaterialService materialService) {
		this.materialService = materialService;
	}

	public List<Supplier> getSupplierList() {
		return supplierList;
	}

	public void setSupplierList(List<Supplier> supplierList) {
		this.supplierList = supplierList;
	}

	public SupplierService getSupplierService() {
		return supplierService;
	}

	public void setSupplierService(SupplierService supplierService) {
		this.supplierService = supplierService;
	}

}
